#!/usr/bin/perl -w

# First release version beta. Takes input file name, exports the MinMax distribution in a MMC- file and the codon index in a CI- file.
# Second version, adding in the DB output file featuring the >ID [MM,MM] name FASTA identifier
# Added Jan 15th 08 

$minpermin = 100;
$perpermin = 0;

#Addendum, adding the window size and setting the last cell of an array
$windowsize = 17;

for ($startpoint = 0; $startpoint < 200; ++$startpoint) {
push(@actminmax,0); 
push(@RRT1,0); push(@RRT2,0); push(@RRT3,0); push(@RRT4,0); push(@RRT5,0); push(@RRT6,0); push(@RRT7,0); push(@RRT8,0); push(@RRT9,0); push(@RRT10,0); push(@RRT11,0); push(@RRT12,0); push(@RRT13,0); push(@RRT14,0); push(@RRT15,0); push(@RRT16,0); push(@RRT17,0); push(@RRT18,0); push(@RRT19,0); push(@RRT20,0); push(@RRT21,0); push(@RRT22,0); push(@RRT23,0); push(@RRT24,0); push(@RRT25,0); push(@RRT26,0); push(@RRT27,0); push(@RRT28,0); push(@RRT29,0); push(@RRT30,0); push(@RRT31,0); push(@RRT32,0); push(@RRT33,0); push(@RRT34,0); push(@RRT35,0); push(@RRT36,0); push(@RRT37,0); push(@RRT38,0); push(@RRT39,0); push(@RRT40,0); push(@RRT41,0); push(@RRT42,0); push(@RRT43,0); push(@RRT44,0); push(@RRT45,0); push(@RRT46,0); push(@RRT47,0); push(@RRT48,0); push(@RRT49,0); push(@RRT50,0); push(@RRT51,0); push(@RRT52,0); push(@RRT53,0); push(@RRT54,0); push(@RRT55,0); push(@RRT56,0); push(@RRT57,0); push(@RRT58,0); push(@RRT59,0); push(@RRT60,0); push(@RRT61,0); push(@RRT62,0); push(@RRT63,0); push(@RRT64,0); push(@RRT65,0); push(@RRT66,0); push(@RRT67,0); push(@RRT68,0); push(@RRT69,0); push(@RRT70,0); push(@RRT71,0); push(@RRT72,0); push(@RRT73,0); push(@RRT74,0); push(@RRT75,0); push(@RRT76,0); push(@RRT77,0); push(@RRT78,0); push(@RRT79,0); push(@RRT80,0); push(@RRT81,0); push(@RRT82,0); push(@RRT83,0); push(@RRT84,0); push(@RRT85,0); push(@RRT86,0); push(@RRT87,0); push(@RRT88,0); push(@RRT89,0); push(@RRT90,0); push(@RRT91,0); push(@RRT92,0); push(@RRT93,0); push(@RRT94,0); push(@RRT95,0); push(@RRT96,0); push(@RRT97,0); push(@RRT98,0); push(@RRT99,0); push(@RRT100,0); push(@RRT101,0); push(@RRT102,0); push(@RRT103,0); push(@RRT104,0); push(@RRT105,0); push(@RRT106,0); push(@RRT107,0); push(@RRT108,0); push(@RRT109,0); push(@RRT110,0); push(@RRT111,0); push(@RRT112,0); push(@RRT113,0); push(@RRT114,0); push(@RRT115,0); push(@RRT116,0); push(@RRT117,0); push(@RRT118,0); push(@RRT119,0); push(@RRT120,0); push(@RRT121,0); push(@RRT122,0); push(@RRT123,0); push(@RRT124,0); push(@RRT125,0); push(@RRT126,0); push(@RRT127,0); push(@RRT128,0); push(@RRT129,0); push(@RRT130,0); push(@RRT131,0); push(@RRT132,0); push(@RRT133,0); push(@RRT134,0); push(@RRT135,0); push(@RRT136,0); push(@RRT137,0); push(@RRT138,0); push(@RRT139,0); push(@RRT140,0); push(@RRT141,0); push(@RRT142,0); push(@RRT143,0); push(@RRT144,0); push(@RRT145,0); push(@RRT146,0); push(@RRT147,0); push(@RRT148,0); push(@RRT149,0); push(@RRT150,0); push(@RRT151,0); push(@RRT152,0); push(@RRT153,0); push(@RRT154,0); push(@RRT155,0); push(@RRT156,0); push(@RRT157,0); push(@RRT158,0); push(@RRT159,0); push(@RRT160,0); push(@RRT161,0); push(@RRT162,0); push(@RRT163,0); push(@RRT164,0); push(@RRT165,0); push(@RRT166,0); push(@RRT167,0); push(@RRT168,0); push(@RRT169,0); push(@RRT170,0); push(@RRT171,0); push(@RRT172,0); push(@RRT173,0); push(@RRT174,0); push(@RRT175,0); push(@RRT176,0); push(@RRT177,0); push(@RRT178,0); push(@RRT179,0); push(@RRT180,0); push(@RRT181,0); push(@RRT182,0); push(@RRT183,0); push(@RRT184,0); push(@RRT185,0); push(@RRT186,0); push(@RRT187,0); push(@RRT188,0); push(@RRT189,0); push(@RRT190,0); push(@RRT191,0); push(@RRT192,0); push(@RRT193,0); push(@RRT194,0); push(@RRT195,0); push(@RRT196,0); push(@RRT197,0); push(@RRT198,0); push(@RRT199,0); push(@RRT200,0); 

}

@RRTsummon = (\@RRT1, \@RRT2, \@RRT3, \@RRT4, \@RRT5, \@RRT6, \@RRT7, \@RRT8, \@RRT9, \@RRT10, \@RRT11, \@RRT12, \@RRT13, \@RRT14, \@RRT15, \@RRT16, \@RRT17, \@RRT18, \@RRT19, \@RRT20, \@RRT21, \@RRT22, \@RRT23, \@RRT24, \@RRT25, \@RRT26, \@RRT27, \@RRT28, \@RRT29, \@RRT30, \@RRT31, \@RRT32, \@RRT33, \@RRT34, \@RRT35, \@RRT36, \@RRT37, \@RRT38, \@RRT39, \@RRT40, \@RRT41, \@RRT42, \@RRT43, \@RRT44, \@RRT45, \@RRT46, \@RRT47, \@RRT48, \@RRT49, \@RRT50, \@RRT51, \@RRT52, \@RRT53, \@RRT54, \@RRT55, \@RRT56, \@RRT57, \@RRT58, \@RRT59, \@RRT60, \@RRT61, \@RRT62, \@RRT63, \@RRT64, \@RRT65, \@RRT66, \@RRT67, \@RRT68, \@RRT69, \@RRT70, \@RRT71, \@RRT72, \@RRT73, \@RRT74, \@RRT75, \@RRT76, \@RRT77, \@RRT78, \@RRT79, \@RRT80, \@RRT81, \@RRT82, \@RRT83, \@RRT84, \@RRT85, \@RRT86, \@RRT87, \@RRT88, \@RRT89, \@RRT90, \@RRT91, \@RRT92, \@RRT93, \@RRT94, \@RRT95, \@RRT96, \@RRT97, \@RRT98, \@RRT99, \@RRT100, \@RRT101, \@RRT102, \@RRT103, \@RRT104, \@RRT105, \@RRT106, \@RRT107, \@RRT108, \@RRT109, \@RRT110, \@RRT111, \@RRT112, \@RRT113, \@RRT114, \@RRT115, \@RRT116, \@RRT117, \@RRT118, \@RRT119, \@RRT120, \@RRT121, \@RRT122, \@RRT123, \@RRT124, \@RRT125, \@RRT126, \@RRT127, \@RRT128, \@RRT129, \@RRT130, \@RRT131, \@RRT132, \@RRT133, \@RRT134, \@RRT135, \@RRT136, \@RRT137, \@RRT138, \@RRT139, \@RRT140, \@RRT141, \@RRT142, \@RRT143, \@RRT144, \@RRT145, \@RRT146, \@RRT147, \@RRT148, \@RRT149, \@RRT150, \@RRT151, \@RRT152, \@RRT153, \@RRT154, \@RRT155, \@RRT156, \@RRT157, \@RRT158, \@RRT159, \@RRT160, \@RRT161, \@RRT162, \@RRT163, \@RRT164, \@RRT165, \@RRT166, \@RRT167, \@RRT168, \@RRT169, \@RRT170, \@RRT171, \@RRT172, \@RRT173, \@RRT174, \@RRT175, \@RRT176, \@RRT177, \@RRT178, \@RRT179, \@RRT180, \@RRT181, \@RRT182, \@RRT183, \@RRT184, \@RRT185, \@RRT186, \@RRT187, \@RRT188, \@RRT189, \@RRT190, \@RRT191, \@RRT192, \@RRT193, \@RRT194, \@RRT195, \@RRT196, \@RRT197, \@RRT198, \@RRT199, \@RRT200);

#Whatever is above here is insanity.  I will bet a day's salary that it is unnecessary and wasteful
# Stage one of program, inputting text from file



#print "Input file name - ";
#$INPUTname = <>;
$INPUTname = $ARGV[0];
chomp $INPUTname;
$FASTAname = $INPUTname;
$outputCI = "CI-" . $INPUTname;
$outputMM = "MMC-" . $INPUTname;
$outputDB = "DB-" . $INPUTname;
$outputGR = "MMV-" . $INPUTname;
$outputDBclick = $INPUTname;
$outputDBclick =~ tr/!.~?//d;
open(PRINTGENOME,">$outputDB");
open(PRINTVALUES,">$outputGR");
# OK, now need to deal with the name of the file.
# A few steps... What I'd like to do is split the file name at the periods, take the extension off to the side, add a note for what type of database and
# then reassemble the pieces

# Replace with the name of the genome or find a attribute definer
# Opening the file
open(FASTAFILE, $FASTAname);

# Array one -names- will be names of the genes
# Array two -sequences- will be the condensed sequences

@names = ();
@sequences = ();

# Start a loop to allow for annotation

while($unprocessed = <FASTAFILE>) {

# Check and see if the first character is a gt

	$checkgt = substr($unprocessed,0,1);

	if ($checkgt eq '>') {
		push (@names, $unprocessed);
		push (@sequences,'');

# If it isn't a gt then it must be more sequences
# so pop off the end of the sequence array
# then get rid of carriage returns in the new seq
# and append it to the end before reintegrating

	} else {
		$old = pop @sequences;
		$unprocessed =~ s/\n//g;
		$unprocessed =~ tr/[a-z]/[A-Z]/;
		$new = $old . $unprocessed;
		push (@sequences,$new);
		}

# Close the while loop
}

# Be tidy!
close FASTAFILE;

# At this point there are two arrays -sequences- and -names-
# These two files are sacrosanct and should never be modified
# Time to count the codons
# Duplicate the array to protect the original stock

# Just checking the length of the array to make sure the printing is correct.

@codoncount = @sequences;
@minmaxcalc = @sequences;

# Clear out variables to that will be used to count

$TTT=0; $TTC=0; $TTA=0; $TTG=0; $CTT=0; $CTC=0; $CTA=0; $CTG=0; $ATT=0; $ATC=0; $ATA=0; $ATG=0; $GTT=0; $GTC=0; $GTA=0; $GTG=0; $TCT=0; $TCC=0; $TCA=0; $TCG=0; $AGT=0; $AGC=0; $CCT=0; $CCC=0; $CCA=0; $CCG=0; $ACT=0; $ACC=0; $ACA=0; $ACG=0; $GCT=0; $GCC=0; $GCA=0; $GCG=0; $TAT=0; $TAC=0; $TAA=0; $TAG=0; $CAT=0; $CAC=0; $CAA=0; $CAG=0; $AAT=0; $AAC=0; $AAA=0; $AAG=0; $GAT=0; $GAC=0; $GAA=0; $GAG=0; $TGT=0; $TGC=0; $TGA=0; $TGG=0; $CGT=0; $CGC=0; $CGA=0; $CGG=0; $AGA=0; $AGG=0; $GGT=0; $GGC=0; $GGA=0; $GGG=0; $badcodon=0; 

# Now the priority is in verifying each gene is accurate
# The best mechanism is the divide by three
# If successful, continue on, else add to the badgene variable

$badgene = 0;

foreach $current (@codoncount) {
	$lengths = length $current;
	$lengthschecka = $lengths / 3;
	$lengthscheckb = int $lengthschecka;

	if ($lengthschecka == $lengthscheckb) {

# Since the length check adds one 
# If it is successful then start the codon counting using substr	

		$counter = 0;
		while ($counter < $lengthschecka) {
			$startpoint = 3 * $counter;
			$codon = substr($current,$startpoint,3);
			
# Elsif bomb...

if ($codon eq 'TTT') {++$TTT;} elsif ($codon eq 'TTC') {++$TTC;} elsif ($codon eq 'TTA') {++$TTA;} elsif ($codon eq 'TTG') {++$TTG;} elsif ($codon eq 'CTT') {++$CTT;} elsif ($codon eq 'CTC') {++$CTC;} elsif ($codon eq 'CTA') {++$CTA;} elsif ($codon eq 'CTG') {++$CTG;} elsif ($codon eq 'ATT') {++$ATT;} elsif ($codon eq 'ATC') {++$ATC;} elsif ($codon eq 'ATA') {++$ATA;} elsif ($codon eq 'ATG') {++$ATG;} elsif ($codon eq 'GTT') {++$GTT;} elsif ($codon eq 'GTC') {++$GTC;} elsif ($codon eq 'GTA') {++$GTA;} elsif ($codon eq 'GTG') {++$GTG;} elsif ($codon eq 'TCT') {++$TCT;} elsif ($codon eq 'TCC') {++$TCC;} elsif ($codon eq 'TCA') {++$TCA;} elsif ($codon eq 'TCG') {++$TCG;} elsif ($codon eq 'AGT') {++$AGT;} elsif ($codon eq 'AGC') {++$AGC;} elsif ($codon eq 'CCT') {++$CCT;} elsif ($codon eq 'CCC') {++$CCC;} elsif ($codon eq 'CCA') {++$CCA;} elsif ($codon eq 'CCG') {++$CCG;} elsif ($codon eq 'ACT') {++$ACT;} elsif ($codon eq 'ACC') {++$ACC;} elsif ($codon eq 'ACA') {++$ACA;} elsif ($codon eq 'ACG') {++$ACG;} elsif ($codon eq 'GCT') {++$GCT;} elsif ($codon eq 'GCC') {++$GCC;} elsif ($codon eq 'GCA') {++$GCA;} elsif ($codon eq 'GCG') {++$GCG;} elsif ($codon eq 'TAT') {++$TAT;} elsif ($codon eq 'TAC') {++$TAC;} elsif ($codon eq 'TAA') {++$TAA;} elsif ($codon eq 'TAG') {++$TAG;} elsif ($codon eq 'CAT') {++$CAT;} elsif ($codon eq 'CAC') {++$CAC;} elsif ($codon eq 'CAA') {++$CAA;} elsif ($codon eq 'CAG') {++$CAG;} elsif ($codon eq 'AAT') {++$AAT;} elsif ($codon eq 'AAC') {++$AAC;} elsif ($codon eq 'AAA') {++$AAA;} elsif ($codon eq 'AAG') {++$AAG;} elsif ($codon eq 'GAT') {++$GAT;} elsif ($codon eq 'GAC') {++$GAC;} elsif ($codon eq 'GAA') {++$GAA;} elsif ($codon eq 'GAG') {++$GAG;} elsif ($codon eq 'TGT') {++$TGT;} elsif ($codon eq 'TGC') {++$TGC;} elsif ($codon eq 'TGA') {++$TGA;} elsif ($codon eq 'TGG') {++$TGG;} elsif ($codon eq 'CGT') {++$CGT;} elsif ($codon eq 'CGC') {++$CGC;} elsif ($codon eq 'CGA') {++$CGA;} elsif ($codon eq 'CGG') {++$CGG;} elsif ($codon eq 'AGA') {++$AGA;} elsif ($codon eq 'AGG') {++$AGG;} elsif ($codon eq 'GGT') {++$GGT;} elsif ($codon eq 'GGC') {++$GGC;} elsif ($codon eq 'GGA') {++$GGA;} elsif ($codon eq 'GGG') {++$GGG;} else {++$badcodon;}

# Uptick the counter, restart the process!

			++$counter;

			}
				
	} else {
# Bad gene monitor
		++$badgene;
		}
	}	

# End codon counting - all nests closed - back to top level

open(INDEXFILE,">$outputCI");

print INDEXFILE "TTT $TTT\n"; print INDEXFILE "TTC $TTC\n"; print INDEXFILE "TTA $TTA\n"; print INDEXFILE "TTG $TTG\n"; print INDEXFILE "CTT $CTT\n"; print INDEXFILE "CTC $CTC\n"; print INDEXFILE "CTA $CTA\n"; print INDEXFILE "CTG $CTG\n"; print INDEXFILE "ATT $ATT\n"; print INDEXFILE "ATC $ATC\n"; print INDEXFILE "ATA $ATA\n"; print INDEXFILE "ATG $ATG\n"; print INDEXFILE "GTT $GTT\n"; print INDEXFILE "GTC $GTC\n"; print INDEXFILE "GTA $GTA\n"; print INDEXFILE "GTG $GTG\n"; print INDEXFILE "TCT $TCT\n"; print INDEXFILE "TCC $TCC\n"; print INDEXFILE "TCA $TCA\n"; print INDEXFILE "TCG $TCG\n"; print INDEXFILE "AGT $AGT\n"; print INDEXFILE "AGC $AGC\n"; print INDEXFILE "CCT $CCT\n"; print INDEXFILE "CCC $CCC\n"; print INDEXFILE "CCA $CCA\n"; print INDEXFILE "CCG $CCG\n"; print INDEXFILE "ACT $ACT\n"; print INDEXFILE "ACC $ACC\n"; print INDEXFILE "ACA $ACA\n"; print INDEXFILE "ACG $ACG\n"; print INDEXFILE "GCT $GCT\n"; print INDEXFILE "GCC $GCC\n"; print INDEXFILE "GCA $GCA\n"; print INDEXFILE "GCG $GCG\n"; print INDEXFILE "TAT $TAT\n"; print INDEXFILE "TAC $TAC\n"; print INDEXFILE "TAA $TAA\n"; print INDEXFILE "TAG $TAG\n"; print INDEXFILE "CAT $CAT\n"; print INDEXFILE "CAC $CAC\n"; print INDEXFILE "CAA $CAA\n"; print INDEXFILE "CAG $CAG\n"; print INDEXFILE "AAT $AAT\n"; print INDEXFILE "AAC $AAC\n"; print INDEXFILE "AAA $AAA\n"; print INDEXFILE "AAG $AAG\n"; print INDEXFILE "GAT $GAT\n"; print INDEXFILE "GAC $GAC\n"; print INDEXFILE "GAA $GAA\n"; print INDEXFILE "GAG $GAG\n"; print INDEXFILE "TGT $TGT\n"; print INDEXFILE "TGC $TGC\n"; print INDEXFILE "TGA $TGA\n"; print INDEXFILE "TGG $TGG\n"; print INDEXFILE "CGT $CGT\n"; print INDEXFILE "CGC $CGC\n"; print INDEXFILE "CGA $CGA\n"; print INDEXFILE "CGG $CGG\n"; print INDEXFILE "AGA $AGA\n"; print INDEXFILE "AGG $AGG\n"; print INDEXFILE "GGT $GGT\n"; print INDEXFILE "GGC $GGC\n"; print INDEXFILE "GGA $GGA\n"; print INDEXFILE "GGG $GGG\n"; print INDEXFILE "badcodon  $badcodon\n"; print INDEXFILE "badgene $badgene\n";

close INDEXFILE;

# Test print bomb

#print "TTT $TTT\n"; print "TTC $TTC\n"; print "TTA $TTA\n"; print "TTG $TTG\n"; print "CTT $CTT\n"; print "CTC $CTC\n"; print "CTA $CTA\n"; print "CTG $CTG\n"; print "ATT $ATT\n"; print "ATC $ATC\n"; print "ATA $ATA\n"; print "ATG $ATG\n"; print "GTT $GTT\n"; print "GTC $GTC\n"; print "GTA $GTA\n"; print "GTG $GTG\n"; print "TCT $TCT\n"; print "TCC $TCC\n"; print "TCA $TCA\n"; print "TCG $TCG\n"; print "AGT $AGT\n"; print "AGC $AGC\n"; print "CCT $CCT\n"; print "CCC $CCC\n"; print "CCA $CCA\n"; print "CCG $CCG\n"; print "ACT $ACT\n"; print "ACC $ACC\n"; print "ACA $ACA\n"; print "ACG $ACG\n"; print "GCT $GCT\n"; print "GCC $GCC\n"; print "GCA $GCA\n"; print "GCG $GCG\n"; print "TAT $TAT\n"; print "TAC $TAC\n"; print "TAA $TAA\n"; print "TAG $TAG\n"; print "CAT $CAT\n"; print "CAC $CAC\n"; print "CAA $CAA\n"; print "CAG $CAG\n"; print "AAT $AAT\n"; print "AAC $AAC\n"; print "AAA $AAA\n"; print "AAG $AAG\n"; print "GAT $GAT\n"; print "GAC $GAC\n"; print "GAA $GAA\n"; print "GAG $GAG\n"; print "TGT $TGT\n"; print "TGC $TGC\n"; print "TGA $TGA\n"; print "TGG $TGG\n"; print "CGT $CGT\n"; print "CGC $CGC\n"; print "CGA $CGA\n"; print "CGG $CGG\n"; print "AGA $AGA\n"; print "AGG $AGG\n"; print "GGT $GGT\n"; print "GGC $GGC\n"; print "GGA $GGA\n"; print "GGG $GGG\n"; print "badcodon  $badcodon\n"; print "badgene $badgene\n";

# Determine min, Z is the stop codon to avoid "." issues

$Fmin = $TTT;
if($TTC < $Fmin) {$Fmin = $TTC;}
$Lmin = $TTA;
if($TTG < $Lmin) {$Lmin = $TTG;}
if($CTT < $Lmin) {$Lmin = $CTT;}
if($CTC < $Lmin) {$Lmin = $CTC;}
if($CTA < $Lmin) {$Lmin = $CTA;}
if($CTG < $Lmin) {$Lmin = $CTG;}
$Imin = $ATT;
if($ATC < $Imin) {$Imin = $ATC;}
if($ATA < $Imin) {$Imin = $ATA;}
$Mmin = $ATG;
$Vmin = $GTT;
if($GTC < $Vmin) {$Vmin = $GTC;}
if($GTA < $Vmin) {$Vmin = $GTA;}
if($GTG < $Vmin) {$Vmin = $GTG;}
$Smin = $TCT;
if($TCC < $Smin) {$Smin = $TCC;}
if($TCA < $Smin) {$Smin = $TCA;}
if($TCG < $Smin) {$Smin = $TCG;}
if($AGT < $Smin) {$Smin = $AGT;}
if($AGC < $Smin) {$Smin = $AGC;}
$Pmin = $CCT;
if($CCC < $Pmin) {$Pmin = $CCC;}
if($CCA < $Pmin) {$Pmin = $CCA;}
if($CCG < $Pmin) {$Pmin = $CCG;}
$Tmin = $ACT;
if($ACC < $Tmin) {$Tmin = $ACC;}
if($ACA < $Tmin) {$Tmin = $ACA;}
if($ACG < $Tmin) {$Tmin = $ACG;}
$Amin = $GCT;
if($GCC < $Amin) {$Amin = $GCC;}
if($GCA < $Amin) {$Amin = $GCA;}
if($GCG < $Amin) {$Amin = $GCG;}
$Ymin = $TAT;
if($TAC < $Ymin) {$Ymin = $TAC;}
$Zmin = $TAA;
if($TAG < $Zmin) {$Zmin = $TAG;}
$Hmin = $CAT;
if($CAC < $Hmin) {$Hmin = $CAC;}
$Qmin = $CAA;
if($CAG < $Qmin) {$Qmin = $CAG;}
$Nmin = $AAT;
if($AAC < $Nmin) {$Nmin = $AAC;}
$Kmin = $AAA;
if($AAG < $Kmin) {$Kmin = $AAG;}
$Dmin = $GAT;
if($GAC < $Dmin) {$Dmin = $GAC;}
$Emin = $GAA;
if($GAG < $Emin) {$Emin = $GAG;}
$Cmin = $TGT;
if($TGC < $Cmin) {$Cmin = $TGC;}
if($TGA < $Zmin) {$Zmin = $TGA;}
$Wmin = $TGG;
$Rmin = $CGT;
if($CGC < $Rmin) {$Rmin = $CGC;}
if($CGA < $Rmin) {$Rmin = $CGA;}
if($CGG < $Rmin) {$Rmin = $CGG;}
if($AGA < $Rmin) {$Rmin = $AGA;}
if($AGG < $Rmin) {$Rmin = $AGG;}
$Gmin = $GGT;
if($GGC < $Gmin) {$Gmin = $GGC;}
if($GGA < $Gmin) {$Gmin = $GGA;}
if($GGG < $Gmin) {$Gmin = $GGG;}

#print "MINS\n";
#print "H\t$Hmin\n"; print "C\t$Cmin\n"; print "Z\t$Zmin\n"; print "Y\t$Ymin\n"; print "A\t$Amin\n"; print "T\t$Tmin\n"; print "P\t$Pmin\n"; print "S\t$Smin\n"; print "V\t$Vmin\n"; print "M\t$Mmin\n"; print "I\t$Imin\n"; print "L\t$Lmin\n"; print "F\t$Fmin\n"; print "Q\t$Qmin\n"; print "K\t$Kmin\n"; print "G\t$Gmin\n"; print "N\t$Nmin\n"; print "D\t$Dmin\n"; print "E\t$Emin\n"; print "W\t$Wmin\n"; print "R\t$Rmin\n";

# Determine reading frame maximum values

$Fmax = $TTT;
if($TTC > $Fmax) {$Fmax = $TTC;}
$Lmax = $TTA;
if($TTG > $Lmax) {$Lmax = $TTG;}
if($CTT > $Lmax) {$Lmax = $CTT;}
if($CTC > $Lmax) {$Lmax = $CTC;}
if($CTA > $Lmax) {$Lmax = $CTA;}
if($CTG > $Lmax) {$Lmax = $CTG;}
$Imax = $ATT;
if($ATC > $Imax) {$Imax = $ATC;}
if($ATA > $Imax) {$Imax = $ATA;}
$Mmax = $ATG;
$Vmax = $GTT;
if($GTC > $Vmax) {$Vmax = $GTC;}
if($GTA > $Vmax) {$Vmax = $GTA;}
if($GTG > $Vmax) {$Vmax = $GTG;}
$Smax = $TCT;
if($TCC > $Smax) {$Smax = $TCC;}
if($TCA > $Smax) {$Smax = $TCA;}
if($TCG > $Smax) {$Smax = $TCG;}
if($AGT > $Smax) {$Smax = $AGT;}
if($AGC > $Smax) {$Smax = $AGC;}
$Pmax = $CCT;
if($CCC > $Pmax) {$Pmax = $CCC;}
if($CCA > $Pmax) {$Pmax = $CCA;}
if($CCG > $Pmax) {$Pmax = $CCG;}
$Tmax = $ACT;
if($ACC > $Tmax) {$Tmax = $ACC;}
if($ACA > $Tmax) {$Tmax = $ACA;}
if($ACG > $Tmax) {$Tmax = $ACG;}
$Amax = $GCT;
if($GCC > $Amax) {$Amax = $GCC;}
if($GCA > $Amax) {$Amax = $GCA;}
if($GCG > $Amax) {$Amax = $GCG;}
$Ymax = $TAT;
if($TAC > $Ymax) {$Ymax = $TAC;}
$Zmax = $TAA;
if($TAG > $Zmax) {$Zmax = $TAG;}
$Hmax = $CAT;
if($CAC > $Hmax) {$Hmax = $CAC;}
$Qmax = $CAA;
if($CAG > $Qmax) {$Qmax = $CAG;}
$Nmax = $AAT;
if($AAC > $Nmax) {$Nmax = $AAC;}
$Kmax = $AAA;
if($AAG > $Kmax) {$Kmax = $AAG;}
$Dmax = $GAT;
if($GAC > $Dmax) {$Dmax = $GAC;}
$Emax = $GAA;
if($GAG > $Emax) {$Emax = $GAG;}
$Cmax = $TGT;
if($TGC > $Cmax) {$Cmax = $TGC;}
if($TGA > $Zmax) {$Zmax = $TGA;}
$Wmax = $TGG;
$Rmax = $CGT;
if($CGC > $Rmax) {$Rmax = $CGC;}
if($CGA > $Rmax) {$Rmax = $CGA;}
if($CGG > $Rmax) {$Rmax = $CGG;}
if($AGA > $Rmax) {$Rmax = $AGA;}
if($AGG > $Rmax) {$Rmax = $AGG;}
$Gmax = $GGT;
if($GGC > $Gmax) {$Gmax = $GGC;}
if($GGA > $Gmax) {$Gmax = $GGA;}
if($GGG > $Gmax) {$Gmax = $GGG;}

#print "MAXS:\n";
#print "H\t$Hmax\n"; print "C\t$Cmax\n"; print "Z\t$Zmax\n"; print "Y\t$Ymax\n"; print "A\t$Amax\n"; print "T\t$Tmax\n"; print "P\t$Pmax\n"; print "S\t$Smax\n"; print "V\t$Vmax\n"; print "M\t$Mmax\n"; print "I\t$Imax\n"; print "L\t$Lmax\n"; print "F\t$Fmax\n"; print "Q\t$Qmax\n"; print "K\t$Kmax\n"; print "G\t$Gmax\n"; print "N\t$Nmax\n"; print "D\t$Dmax\n"; print "E\t$Emax\n"; print "W\t$Wmax\n"; print "R\t$Rmax\n";
# Previously printbombed the file, worked perfectly - see Excel file

# Until the phew comment, everything is still Min Max Bomb

# Phew.
# Now to calculate the averages.

$Favg = 0; $Lavg = 0; $Iavg = 0; $Mavg = 0; $Vavg = 0; $Savg = 0; $Pavg = 0; $Tavg = 0; $Aavg = 0; $Yavg = 0; $Zavg = 0; $Havg = 0; $Qavg = 0; $Navg = 0; $Kavg = 0; $Davg = 0; $Eavg = 0; $Cavg = 0; $Wavg = 0; $Ravg = 0; $Gavg = 0;  $Favg = ( $TTT / 2) + $Favg;  $Favg = ( $TTC / 2) + $Favg; $Lavg = ( $TTA / 6) + $Lavg; $Lavg = ( $TTG / 6) + $Lavg; $Lavg = ( $CTT / 6) + $Lavg; $Lavg = ( $CTC / 6) + $Lavg; $Lavg = ( $CTA / 6) + $Lavg; $Lavg = ( $CTG / 6) + $Lavg; $Iavg = ( $ATT / 3) + $Iavg; $Iavg = ( $ATC / 3) + $Iavg; $Iavg = ( $ATA / 3) + $Iavg; $Mavg = ( $ATG / 1) + $Mavg; $Vavg = ( $GTT / 4) + $Vavg; $Vavg = ( $GTC / 4) + $Vavg; $Vavg = ( $GTA / 4) + $Vavg; $Vavg = ( $GTG / 4) + $Vavg; $Savg = ( $TCT / 6) + $Savg; $Savg = ( $TCC / 6) + $Savg; $Savg = ( $TCA / 6) + $Savg; $Savg = ( $TCG / 6) + $Savg; $Savg = ( $AGT / 6) + $Savg; $Savg = ( $AGC / 6) + $Savg; $Pavg = ( $CCT / 4) + $Pavg; $Pavg = ( $CCC / 4) + $Pavg; $Pavg = ( $CCA / 4) + $Pavg; $Pavg = ( $CCG / 4) + $Pavg; $Tavg = ( $ACT / 4) + $Tavg; $Tavg = ( $ACC / 4) + $Tavg; $Tavg = ( $ACA / 4) + $Tavg; $Tavg = ( $ACG / 4) + $Tavg; $Aavg = ( $GCT / 4) + $Aavg; $Aavg = ( $GCC / 4) + $Aavg; $Aavg = ( $GCA / 4) + $Aavg; $Aavg = ( $GCG / 4) + $Aavg; $Yavg = ( $TAT / 2) + $Yavg; $Yavg = ( $TAC / 2) + $Yavg; $Zavg = ( $TAA / 3) + $Zavg; $Zavg = ( $TAG / 3) + $Zavg; $Havg = ( $CAT / 2) + $Havg; $Havg = ( $CAC / 2) + $Havg; $Qavg = ( $CAA / 2) + $Qavg; $Qavg = ( $CAG / 2) + $Qavg; $Navg = ( $AAT / 2) + $Navg; $Navg = ( $AAC / 2) + $Navg; $Kavg = ( $AAA / 2) + $Kavg; $Kavg = ( $AAG / 2) + $Kavg; $Davg = ( $GAT / 2) + $Davg; $Davg = ( $GAC / 2) + $Davg; $Eavg = ( $GAA / 2) + $Eavg; $Eavg = ( $GAG / 2) + $Eavg; $Cavg = ( $TGT / 2) + $Cavg; $Cavg = ( $TGC / 2) + $Cavg; $Zavg = ( $TGA / 3) + $Zavg; $Wavg = ( $TGG / 1) + $Wavg; $Ravg = ( $CGT / 6) + $Ravg; $Ravg = ( $CGC / 6) + $Ravg; $Ravg = ( $CGA / 6) + $Ravg; $Ravg = ( $CGG / 6) + $Ravg; $Ravg = ( $AGA / 6) + $Ravg; $Ravg = ( $AGG / 6) + $Ravg; $Gavg = ( $GGT / 4) + $Gavg; $Gavg = ( $GGC / 4) + $Gavg; $Gavg = ( $GGA / 4) + $Gavg; $Gavg = ( $GGG / 4) + $Gavg;

#print "AVGS:\n";
#print "H\t$Havg\n"; print "C\t$Cavg\n"; print "Z\t$Zavg\n"; print "Y\t$Yavg\n"; print "A\t$Aavg\n"; print "T\t$Tavg\n"; print "P\t$Pavg\n"; print "S\t$Savg\n"; print "V\t$Vavg\n"; print "M\t$Mavg\n"; print "I\t$Iavg\n"; print "L\t$Lavg\n"; print "F\t$Favg\n"; print "Q\t$Qavg\n"; print "K\t$Kavg\n"; print "G\t$Gavg\n"; print "N\t$Navg\n"; print "D\t$Davg\n"; print "E\t$Eavg\n"; print "W\t$Wavg\n"; print "R\t$Ravg\n";
# I'm sure that's clear to anyone who is reading this. 
# It's simply counting all the codons using the ++ to keep track, 
# then creating the min max and avg for the three frames

# Now for the tough part: counting the primary reading frame, the secondary reading frames and all 200 reverse translations
# and all shuffle reverse translations. 

@rrtcalc  = @minmaxcalc;

$outputDBcounter = 0;
# First run, output for the standard %MM as a control, copying up to the next comment from above
while (@minmaxcalc) {
	++$outputDBcounter;
	$outputDBprint = '';
	$outputGRprint = '';
	$current = shift @minmaxcalc;
	$currentgene = $current;
	$currentname = shift @names;
	$currentname =~ s/\n//g;
	$lengths = length $current;
	$lengthschecka = $lengths / 3;
	$lengthscheckb = int $lengthschecka;
	if ($lengthschecka == $lengthscheckb) {
		$counter = 0;
		while ($counter < ($lengthschecka-$windowsize) ) {
			$startpoint = 3 * $counter;
			$minWin = 0;$maxWin = 0;$avgWin = 0;$actWin = 0;
			for ($startpoint = 3 * $counter; $startpoint < (3 * ($counter + $windowsize)); $startpoint += 3) {
				$codon = substr($current,$startpoint,3);
					if($codon eq 'TTT') {$minWin += $Fmin; $maxWin += $Fmax; $avgWin += $Favg; $actWin += $TTT;}

					elsif($codon eq 'TTC') {$minWin += $Fmin; $maxWin += $Fmax; $avgWin += $Favg; $actWin += $TTC;}
					elsif($codon eq 'TTA') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $TTA;}
					elsif($codon eq 'TTG') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $TTG;}
					elsif($codon eq 'CTT') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTT;}
					elsif($codon eq 'CTC') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTC;}
					elsif($codon eq 'CTA') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTA;}
					elsif($codon eq 'CTG') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTG;}
					elsif($codon eq 'ATT') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATT;}
					elsif($codon eq 'ATC') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATC;}
					elsif($codon eq 'ATA') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATA;}
					elsif($codon eq 'ATG') {$minWin += $Mmin; $maxWin += $Mmax; $avgWin += $Mavg; $actWin += $ATG;}
					elsif($codon eq 'GTT') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTT;}
					elsif($codon eq 'GTC') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTC;}
					elsif($codon eq 'GTA') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTA;}
					elsif($codon eq 'GTG') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTG;}
					elsif($codon eq 'TCT') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCT;}
					elsif($codon eq 'TCC') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCC;}
					elsif($codon eq 'TCA') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCA;}
					elsif($codon eq 'TCG') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCG;}
					elsif($codon eq 'AGT') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $AGT;}
					elsif($codon eq 'AGC') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $AGC;}
					elsif($codon eq 'CCT') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCT;}

					elsif($codon eq 'CCC') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCC;}

					elsif($codon eq 'CCA') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCA;}
					elsif($codon eq 'CCG') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCG;}
					elsif($codon eq 'ACT') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACT;}
					elsif($codon eq 'ACC') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACC;}
					elsif($codon eq 'ACA') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACA;}
					elsif($codon eq 'ACG') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACG;}
					elsif($codon eq 'GCT') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCT;}
					elsif($codon eq 'GCC') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCC;}
					elsif($codon eq 'GCA') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCA;}
					elsif($codon eq 'GCG') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCG;}
					elsif($codon eq 'TAT') {$minWin += $Ymin; $maxWin += $Ymax; $avgWin += $Yavg; $actWin += $TAT;}
					elsif($codon eq 'TAC') {$minWin += $Ymin; $maxWin += $Ymax; $avgWin += $Yavg; $actWin += $TAC;}
					elsif($codon eq 'TAA') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TAA;}
					elsif($codon eq 'TAG') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TAG;}
					elsif($codon eq 'CAT') {$minWin += $Hmin; $maxWin += $Hmax; $avgWin += $Havg; $actWin += $CAT;}
					elsif($codon eq 'CAC') {$minWin += $Hmin; $maxWin += $Hmax; $avgWin += $Havg; $actWin += $CAC;}
					elsif($codon eq 'CAA') {$minWin += $Qmin; $maxWin += $Qmax; $avgWin += $Qavg; $actWin += $CAA;}
					elsif($codon eq 'CAG') {$minWin += $Qmin; $maxWin += $Qmax; $avgWin += $Qavg; $actWin += $CAG;}
					elsif($codon eq 'AAT') {$minWin += $Nmin; $maxWin += $Nmax; $avgWin += $Navg; $actWin += $AAT;}
					elsif($codon eq 'AAC') {$minWin += $Nmin; $maxWin += $Nmax; $avgWin += $Navg; $actWin += $AAC;}
					elsif($codon eq 'AAA') {$minWin += $Kmin; $maxWin += $Kmax; $avgWin += $Kavg; $actWin += $AAA;}
					elsif($codon eq 'AAG') {$minWin += $Kmin; $maxWin += $Kmax; $avgWin += $Kavg; $actWin += $AAG;}
					elsif($codon eq 'GAT') {$minWin += $Dmin; $maxWin += $Dmax; $avgWin += $Davg; $actWin += $GAT;}
					elsif($codon eq 'GAC') {$minWin += $Dmin; $maxWin += $Dmax; $avgWin += $Davg; $actWin += $GAC;}
					elsif($codon eq 'GAA') {$minWin += $Emin; $maxWin += $Emax; $avgWin += $Eavg; $actWin += $GAA;}
					elsif($codon eq 'GAG') {$minWin += $Emin; $maxWin += $Emax; $avgWin += $Eavg; $actWin += $GAG;}
					elsif($codon eq 'TGT') {$minWin += $Cmin; $maxWin += $Cmax; $avgWin += $Cavg; $actWin += $TGT;}
					elsif($codon eq 'TGC') {$minWin += $Cmin; $maxWin += $Cmax; $avgWin += $Cavg; $actWin += $TGC;}
					elsif($codon eq 'TGA') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TGA;}
					elsif($codon eq 'TGG') {$minWin += $Wmin; $maxWin += $Wmax; $avgWin += $Wavg; $actWin += $TGG;}
					elsif($codon eq 'CGT') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGT;}
					elsif($codon eq 'CGC') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGC;}
					elsif($codon eq 'CGA') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGA;}
					elsif($codon eq 'CGG') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGG;}
					elsif($codon eq 'AGA') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $AGA;}
					elsif($codon eq 'AGG') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $AGG;}
					elsif($codon eq 'GGT') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGT;}	
					elsif($codon eq 'GGC') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGC;}
					elsif($codon eq 'GGA') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGA;}
					elsif($codon eq 'GGG') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGG;}

				}

# This entire for loop simply calculates the %MM codon by codon for the window. Now for the math...

			if($actWin eq $avgWin) {$perMinMax = 0;}
			elsif($actWin > $avgWin) {$perMinMax = ($actWin - $avgWin) / ($maxWin - $avgWin);}
			elsif($actWin < $avgWin) {$perMinMax = ($actWin - $avgWin) / ($avgWin - $minWin);}
			$outputGRint = int( 10000 * $perMinMax ) / 100;
			$cell = int( 100 * $perMinMax ) + 100;
			$freecomma = ',';
			$outputGRprint = $outputGRprint . $outputGRint . $freecomma;
# Added Jan 15 08 The percent percent min and min percent min region

			if($perMinMax < $minpermin) {$minpermin = $perMinMax;}
			if($perMinMax < 0) {++$perpermin;}

# End addition Jan 15 08

			++$actminmax[$cell];
			++$counter;
		}

if ($counter > 0) {$perpermin = $perpermin / $counter;}
$perpermin = int(100 * $perpermin);
$minpermin = int(100 * $minpermin);

$freezero = '0';
if ($outputDBcounter < 100000) {$outputDBprint = $outputDBprint . $freezero;}
if ($outputDBcounter < 10000) {$outputDBprint = $outputDBprint . $freezero;}
if ($outputDBcounter < 1000) {$outputDBprint = $outputDBprint . $freezero;}
if ($outputDBcounter < 100) {$outputDBprint = $outputDBprint . $freezero;}
if ($outputDBcounter < 10) {$outputDBprint = $outputDBprint . $freezero;}
$outputDBprint =  $outputDBprint . $outputDBcounter;
$outputDBprint = $outputDBclick . $outputDBprint;

@outputtitle = split(/ /,$currentname);
$quicksave = shift @outputtitle;
$outputprint = join(" ",@outputtitle);
$quicksave =~ tr/>//d;

print PRINTGENOME ">$outputDBprint [$perpermin,$minpermin] $quicksave $outputprint\n$currentgene\n";
print PRINTVALUES ">$outputDBprint [$perpermin,$minpermin] $quicksave $outputprint\n$outputGRprint\n";

$perpermin = 0;
$minpermin = 100;


# This closes the while loop and moves to the next point using the counter variable

}
# Closes the "if" for good gene / bad gene

}

# Closes the "foreach" which moves between genes
foreach $printer (@actminmax) {
print "$printer\n";
}
print "Finished primary frame";

close PRINTGENOME;

#
#
#
#
# The insertion of the RRT (not shuffle)
#
#
#
#
#
#
srand(time|$$);

# Seeding the random variable

# Insert the entire calculation after that...

$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TTT; ++$randomcounter) {push(@Fgenerand,'TTT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TTC; ++$randomcounter) {push(@Fgenerand,'TTC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TTA; ++$randomcounter) {push(@Lgenerand,'TTA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TTG; ++$randomcounter) {push(@Lgenerand,'TTG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CTT; ++$randomcounter) {push(@Lgenerand,'CTT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CTC; ++$randomcounter) {push(@Lgenerand,'CTC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CTA; ++$randomcounter) {push(@Lgenerand,'CTA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CTG; ++$randomcounter) {push(@Lgenerand,'CTG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ATT; ++$randomcounter) {push(@Igenerand,'ATT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ATC; ++$randomcounter) {push(@Igenerand,'ATC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ATA; ++$randomcounter) {push(@Igenerand,'ATA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ATG; ++$randomcounter) {push(@Mgenerand,'ATG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GTT; ++$randomcounter) {push(@Vgenerand,'GTT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GTC; ++$randomcounter) {push(@Vgenerand,'GTC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GTA; ++$randomcounter) {push(@Vgenerand,'GTA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GTG; ++$randomcounter) {push(@Vgenerand,'GTG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TCT; ++$randomcounter) {push(@Sgenerand,'TCT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TCC; ++$randomcounter) {push(@Sgenerand,'TCC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TCA; ++$randomcounter) {push(@Sgenerand,'TCA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TCG; ++$randomcounter) {push(@Sgenerand,'TCG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AGT; ++$randomcounter) {push(@Sgenerand,'AGT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AGC; ++$randomcounter) {push(@Sgenerand,'AGC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CCT; ++$randomcounter) {push(@Pgenerand,'CCT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CCC; ++$randomcounter) {push(@Pgenerand,'CCC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CCA; ++$randomcounter) {push(@Pgenerand,'CCA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CCG; ++$randomcounter) {push(@Pgenerand,'CCG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ACT; ++$randomcounter) {push(@Tgenerand,'ACT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ACC; ++$randomcounter) {push(@Tgenerand,'ACC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ACA; ++$randomcounter) {push(@Tgenerand,'ACA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $ACG; ++$randomcounter) {push(@Tgenerand,'ACG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GCT; ++$randomcounter) {push(@Agenerand,'GCT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GCC; ++$randomcounter) {push(@Agenerand,'GCC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GCA; ++$randomcounter) {push(@Agenerand,'GCA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GCG; ++$randomcounter) {push(@Agenerand,'GCG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TAT; ++$randomcounter) {push(@Ygenerand,'TAT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TAC; ++$randomcounter) {push(@Ygenerand,'TAC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TAA; ++$randomcounter) {push(@Zgenerand,'TAA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TAG; ++$randomcounter) {push(@Zgenerand,'TAG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CAT; ++$randomcounter) {push(@Hgenerand,'CAT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CAC; ++$randomcounter) {push(@Hgenerand,'CAC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CAA; ++$randomcounter) {push(@Qgenerand,'CAA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CAG; ++$randomcounter) {push(@Qgenerand,'CAG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AAT; ++$randomcounter) {push(@Ngenerand,'AAT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AAC; ++$randomcounter) {push(@Ngenerand,'AAC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AAA; ++$randomcounter) {push(@Kgenerand,'AAA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AAG; ++$randomcounter) {push(@Kgenerand,'AAG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GAT; ++$randomcounter) {push(@Dgenerand,'GAT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GAC; ++$randomcounter) {push(@Dgenerand,'GAC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GAA; ++$randomcounter) {push(@Egenerand,'GAA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GAG; ++$randomcounter) {push(@Egenerand,'GAG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TGT; ++$randomcounter) {push(@Cgenerand,'TGT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TGC; ++$randomcounter) {push(@Cgenerand,'TGC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TGA; ++$randomcounter) {push(@Zgenerand,'TGA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $TGG; ++$randomcounter) {push(@Wgenerand,'TGG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CGT; ++$randomcounter) {push(@Rgenerand,'CGT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CGC; ++$randomcounter) {push(@Rgenerand,'CGC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CGA; ++$randomcounter) {push(@Rgenerand,'CGA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $CGG; ++$randomcounter) {push(@Rgenerand,'CGG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AGA; ++$randomcounter) {push(@Rgenerand,'AGA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $AGG; ++$randomcounter) {push(@Rgenerand,'AGG');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GGT; ++$randomcounter) {push(@Ggenerand,'GGT');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GGC; ++$randomcounter) {push(@Ggenerand,'GGC');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GGA; ++$randomcounter) {push(@Ggenerand,'GGA');}
$randomcounter = 0; for ($randomcounter = 0; $randomcounter < $GGG; ++$randomcounter) {push(@Ggenerand,'GGG');}


foreach $current (@rrtcalc) {

	$lengths = length $current;
	$lengthschecka = $lengths / 3;
	$lengthscheckb = int $lengthschecka;

	if ($lengthschecka == $lengthscheckb) {

# Since the length check adds one 
# If it is successful then start the codon counting using substr	

#The preceding bracket closes the while loop for the first time around, still in the gene check if statement

for ($rrtsort = 0; $rrtsort < 200; ++$rrtsort) {
@newgene = ();

		$counterb = 0;
		while ($counterb < $lengthschecka) {
		$startpoint = 3 * $counterb;
		$origcodon = substr($current,$startpoint,3);
		if($origcodon eq 'TTT') {$randomcont = int(rand(scalar @Fgenerand)); $codon = $Fgenerand[$randomcont];}
		elsif($origcodon eq 'TTC') {$randomcont = int(rand(scalar @Fgenerand)); $codon = $Fgenerand[$randomcont];}
		elsif($origcodon eq 'TTA') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'TTG') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'CTT') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'CTC') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'CTA') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'CTG') {$randomcont = int(rand(scalar @Lgenerand)); $codon = $Lgenerand[$randomcont];}
		elsif($origcodon eq 'ATT') {$randomcont = int(rand(scalar @Igenerand)); $codon = $Igenerand[$randomcont];}
		elsif($origcodon eq 'ATC') {$randomcont = int(rand(scalar @Igenerand)); $codon = $Igenerand[$randomcont];}
		elsif($origcodon eq 'ATA') {$randomcont = int(rand(scalar @Igenerand)); $codon = $Igenerand[$randomcont];}
		elsif($origcodon eq 'ATG') {$randomcont = int(rand(scalar @Mgenerand)); $codon = $Mgenerand[$randomcont];}
		elsif($origcodon eq 'GTT') {$randomcont = int(rand(scalar @Vgenerand)); $codon = $Vgenerand[$randomcont];}
		elsif($origcodon eq 'GTC') {$randomcont = int(rand(scalar @Vgenerand)); $codon = $Vgenerand[$randomcont];}
		elsif($origcodon eq 'GTA') {$randomcont = int(rand(scalar @Vgenerand)); $codon = $Vgenerand[$randomcont];}
		elsif($origcodon eq 'GTG') {$randomcont = int(rand(scalar @Vgenerand)); $codon = $Vgenerand[$randomcont];}
		elsif($origcodon eq 'TCT') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'TCC') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'TCA') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'TCG') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'AGT') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'AGC') {$randomcont = int(rand(scalar @Sgenerand)); $codon = $Sgenerand[$randomcont];}
		elsif($origcodon eq 'CCT') {$randomcont = int(rand(scalar @Pgenerand)); $codon = $Pgenerand[$randomcont];}
		elsif($origcodon eq 'CCC') {$randomcont = int(rand(scalar @Pgenerand)); $codon = $Pgenerand[$randomcont];}
		elsif($origcodon eq 'CCA') {$randomcont = int(rand(scalar @Pgenerand)); $codon = $Pgenerand[$randomcont];}
		elsif($origcodon eq 'CCG') {$randomcont = int(rand(scalar @Pgenerand)); $codon = $Pgenerand[$randomcont];}
		elsif($origcodon eq 'ACT') {$randomcont = int(rand(scalar @Tgenerand)); $codon = $Tgenerand[$randomcont];}
		elsif($origcodon eq 'ACC') {$randomcont = int(rand(scalar @Tgenerand)); $codon = $Tgenerand[$randomcont];}
		elsif($origcodon eq 'ACA') {$randomcont = int(rand(scalar @Tgenerand)); $codon = $Tgenerand[$randomcont];}
		elsif($origcodon eq 'ACG') {$randomcont = int(rand(scalar @Tgenerand)); $codon = $Tgenerand[$randomcont];}
		elsif($origcodon eq 'GCT') {$randomcont = int(rand(scalar @Agenerand)); $codon = $Agenerand[$randomcont];}
		elsif($origcodon eq 'GCC') {$randomcont = int(rand(scalar @Agenerand)); $codon = $Agenerand[$randomcont];}
		elsif($origcodon eq 'GCA') {$randomcont = int(rand(scalar @Agenerand)); $codon = $Agenerand[$randomcont];}
		elsif($origcodon eq 'GCG') {$randomcont = int(rand(scalar @Agenerand)); $codon = $Agenerand[$randomcont];}
		elsif($origcodon eq 'TAT') {$randomcont = int(rand(scalar @Ygenerand)); $codon = $Ygenerand[$randomcont];}
		elsif($origcodon eq 'TAC') {$randomcont = int(rand(scalar @Ygenerand)); $codon = $Ygenerand[$randomcont];}
		elsif($origcodon eq 'TAA') {$randomcont = int(rand(scalar @Zgenerand)); $codon = $Zgenerand[$randomcont];}
		elsif($origcodon eq 'TAG') {$randomcont = int(rand(scalar @Zgenerand)); $codon = $Zgenerand[$randomcont];}
		elsif($origcodon eq 'CAT') {$randomcont = int(rand(scalar @Hgenerand)); $codon = $Hgenerand[$randomcont];}
		elsif($origcodon eq 'CAC') {$randomcont = int(rand(scalar @Hgenerand)); $codon = $Hgenerand[$randomcont];}
		elsif($origcodon eq 'CAA') {$randomcont = int(rand(scalar @Qgenerand)); $codon = $Qgenerand[$randomcont];}
		elsif($origcodon eq 'CAG') {$randomcont = int(rand(scalar @Qgenerand)); $codon = $Qgenerand[$randomcont];}
		elsif($origcodon eq 'AAT') {$randomcont = int(rand(scalar @Ngenerand)); $codon = $Ngenerand[$randomcont];}
		elsif($origcodon eq 'AAC') {$randomcont = int(rand(scalar @Ngenerand)); $codon = $Ngenerand[$randomcont];}
		elsif($origcodon eq 'AAA') {$randomcont = int(rand(scalar @Kgenerand)); $codon = $Kgenerand[$randomcont];}
		elsif($origcodon eq 'AAG') {$randomcont = int(rand(scalar @Kgenerand)); $codon = $Kgenerand[$randomcont];}
		elsif($origcodon eq 'GAT') {$randomcont = int(rand(scalar @Dgenerand)); $codon = $Dgenerand[$randomcont];}
		elsif($origcodon eq 'GAC') {$randomcont = int(rand(scalar @Dgenerand)); $codon = $Dgenerand[$randomcont];}
		elsif($origcodon eq 'GAA') {$randomcont = int(rand(scalar @Egenerand)); $codon = $Egenerand[$randomcont];}
		elsif($origcodon eq 'GAG') {$randomcont = int(rand(scalar @Egenerand)); $codon = $Egenerand[$randomcont];}
		elsif($origcodon eq 'TGT') {$randomcont = int(rand(scalar @Cgenerand)); $codon = $Cgenerand[$randomcont];}
		elsif($origcodon eq 'TGC') {$randomcont = int(rand(scalar @Cgenerand)); $codon = $Cgenerand[$randomcont];}
		elsif($origcodon eq 'TGA') {$randomcont = int(rand(scalar @Zgenerand)); $codon = $Zgenerand[$randomcont];}
		elsif($origcodon eq 'TGG') {$randomcont = int(rand(scalar @Wgenerand)); $codon = $Wgenerand[$randomcont];}
		elsif($origcodon eq 'CGT') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'CGC') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'CGA') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'CGG') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'AGA') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'AGG') {$randomcont = int(rand(scalar @Rgenerand)); $codon = $Rgenerand[$randomcont];}
		elsif($origcodon eq 'GGT') {$randomcont = int(rand(scalar @Ggenerand)); $codon = $Ggenerand[$randomcont];}
		elsif($origcodon eq 'GGC') {$randomcont = int(rand(scalar @Ggenerand)); $codon = $Ggenerand[$randomcont];}
		elsif($origcodon eq 'GGA') {$randomcont = int(rand(scalar @Ggenerand)); $codon = $Ggenerand[$randomcont];}
		elsif($origcodon eq 'GGG') {$randomcont = int(rand(scalar @Ggenerand)); $codon = $Ggenerand[$randomcont];}


				push(@newgene, $codon);
			++$counterb;
		}
				$currentrrt = join("",@newgene);

# This ends the second while loop that made the new gene, still need to turn the gene from an array to a scalar for %MM

		$counter = 0;
		while ($counter < ($lengthschecka-$windowsize) ) {
			$startpoint = 3 * $counter;
			$minWin = 0;$maxWin = 0;$avgWin = 0;$actWin = 0;
			for ($startpoint = 3 * $counter; $startpoint < (3 * ($counter + $windowsize)); $startpoint += 3) {
				$codon = substr($currentrrt,$startpoint,3);
					if($codon eq 'TTT') {$minWin += $Fmin; $maxWin += $Fmax; $avgWin += $Favg; $actWin += $TTT;}

					elsif($codon eq 'TTC') {$minWin += $Fmin; $maxWin += $Fmax; $avgWin += $Favg; $actWin += $TTC;}
					elsif($codon eq 'TTA') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $TTA;}
					elsif($codon eq 'TTG') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $TTG;}
					elsif($codon eq 'CTT') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTT;}
					elsif($codon eq 'CTC') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTC;}
					elsif($codon eq 'CTA') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTA;}
					elsif($codon eq 'CTG') {$minWin += $Lmin; $maxWin += $Lmax; $avgWin += $Lavg; $actWin += $CTG;}
					elsif($codon eq 'ATT') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATT;}
					elsif($codon eq 'ATC') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATC;}
					elsif($codon eq 'ATA') {$minWin += $Imin; $maxWin += $Imax; $avgWin += $Iavg; $actWin += $ATA;}
					elsif($codon eq 'ATG') {$minWin += $Mmin; $maxWin += $Mmax; $avgWin += $Mavg; $actWin += $ATG;}
					elsif($codon eq 'GTT') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTT;}
					elsif($codon eq 'GTC') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTC;}
					elsif($codon eq 'GTA') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTA;}
					elsif($codon eq 'GTG') {$minWin += $Vmin; $maxWin += $Vmax; $avgWin += $Vavg; $actWin += $GTG;}
					elsif($codon eq 'TCT') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCT;}
					elsif($codon eq 'TCC') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCC;}
					elsif($codon eq 'TCA') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCA;}
					elsif($codon eq 'TCG') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $TCG;}
					elsif($codon eq 'AGT') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $AGT;}
					elsif($codon eq 'AGC') {$minWin += $Smin; $maxWin += $Smax; $avgWin += $Savg; $actWin += $AGC;}
					elsif($codon eq 'CCT') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCT;}

					elsif($codon eq 'CCC') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCC;}

					elsif($codon eq 'CCA') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCA;}
					elsif($codon eq 'CCG') {$minWin += $Pmin; $maxWin += $Pmax; $avgWin += $Pavg; $actWin += $CCG;}
					elsif($codon eq 'ACT') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACT;}
					elsif($codon eq 'ACC') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACC;}
					elsif($codon eq 'ACA') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACA;}
					elsif($codon eq 'ACG') {$minWin += $Tmin; $maxWin += $Tmax; $avgWin += $Tavg; $actWin += $ACG;}
					elsif($codon eq 'GCT') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCT;}
					elsif($codon eq 'GCC') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCC;}
					elsif($codon eq 'GCA') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCA;}
					elsif($codon eq 'GCG') {$minWin += $Amin; $maxWin += $Amax; $avgWin += $Aavg; $actWin += $GCG;}
					elsif($codon eq 'TAT') {$minWin += $Ymin; $maxWin += $Ymax; $avgWin += $Yavg; $actWin += $TAT;}
					elsif($codon eq 'TAC') {$minWin += $Ymin; $maxWin += $Ymax; $avgWin += $Yavg; $actWin += $TAC;}
					elsif($codon eq 'TAA') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TAA;}
					elsif($codon eq 'TAG') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TAG;}
					elsif($codon eq 'CAT') {$minWin += $Hmin; $maxWin += $Hmax; $avgWin += $Havg; $actWin += $CAT;}
					elsif($codon eq 'CAC') {$minWin += $Hmin; $maxWin += $Hmax; $avgWin += $Havg; $actWin += $CAC;}
					elsif($codon eq 'CAA') {$minWin += $Qmin; $maxWin += $Qmax; $avgWin += $Qavg; $actWin += $CAA;}
					elsif($codon eq 'CAG') {$minWin += $Qmin; $maxWin += $Qmax; $avgWin += $Qavg; $actWin += $CAG;}
					elsif($codon eq 'AAT') {$minWin += $Nmin; $maxWin += $Nmax; $avgWin += $Navg; $actWin += $AAT;}
					elsif($codon eq 'AAC') {$minWin += $Nmin; $maxWin += $Nmax; $avgWin += $Navg; $actWin += $AAC;}
					elsif($codon eq 'AAA') {$minWin += $Kmin; $maxWin += $Kmax; $avgWin += $Kavg; $actWin += $AAA;}
					elsif($codon eq 'AAG') {$minWin += $Kmin; $maxWin += $Kmax; $avgWin += $Kavg; $actWin += $AAG;}
					elsif($codon eq 'GAT') {$minWin += $Dmin; $maxWin += $Dmax; $avgWin += $Davg; $actWin += $GAT;}
					elsif($codon eq 'GAC') {$minWin += $Dmin; $maxWin += $Dmax; $avgWin += $Davg; $actWin += $GAC;}
					elsif($codon eq 'GAA') {$minWin += $Emin; $maxWin += $Emax; $avgWin += $Eavg; $actWin += $GAA;}
					elsif($codon eq 'GAG') {$minWin += $Emin; $maxWin += $Emax; $avgWin += $Eavg; $actWin += $GAG;}
					elsif($codon eq 'TGT') {$minWin += $Cmin; $maxWin += $Cmax; $avgWin += $Cavg; $actWin += $TGT;}
					elsif($codon eq 'TGC') {$minWin += $Cmin; $maxWin += $Cmax; $avgWin += $Cavg; $actWin += $TGC;}
					elsif($codon eq 'TGA') {$minWin += $Zmin; $maxWin += $Zmax; $avgWin += $Zavg; $actWin += $TGA;}
					elsif($codon eq 'TGG') {$minWin += $Wmin; $maxWin += $Wmax; $avgWin += $Wavg; $actWin += $TGG;}
					elsif($codon eq 'CGT') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGT;}
					elsif($codon eq 'CGC') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGC;}
					elsif($codon eq 'CGA') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGA;}
					elsif($codon eq 'CGG') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $CGG;}
					elsif($codon eq 'AGA') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $AGA;}
					elsif($codon eq 'AGG') {$minWin += $Rmin; $maxWin += $Rmax; $avgWin += $Ravg; $actWin += $AGG;}
					elsif($codon eq 'GGT') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGT;}	
					elsif($codon eq 'GGC') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGC;}
					elsif($codon eq 'GGA') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGA;}
					elsif($codon eq 'GGG') {$minWin += $Gmin; $maxWin += $Gmax; $avgWin += $Gavg; $actWin += $GGG;}

				}

# This entire for loop simply calculates the %MM codon by codon for the window. Now for the math...

			if($actWin eq $avgWin) {$perMinMax = 0;}
			elsif($actWin > $avgWin) {$perMinMax = ($actWin - $avgWin) / ($maxWin - $avgWin);}
			elsif($actWin < $avgWin) {$perMinMax = ($actWin - $avgWin) / ($avgWin - $minWin);}
			$cell = int( 100 * $perMinMax ) + 100;
			++$RRTsummon[$rrtsort][$cell];
			++$counter;
		}



	}

# The preceding closes the 200rrt loop

	}

# This ends the if statement, moving out of the gene check

}
$oogabooga = 0;

open(COUNTBASE, ">$outputMM");


for ( $mmvalue = 0; $mmvalue < 200; ++$mmvalue ) {
print COUNTBASE "Min Max designation $mmvalue, $actminmax[$mmvalue], ";

for ( $coordin = 0; $coordin < 200; ++$coordin ) { 
print COUNTBASE $RRTsummon[$coordin][$mmvalue];
print COUNTBASE ", ";
}

print COUNTBASE "End value line $mmvalue\n";

}
close COUNTBASE;
exit;
